package cn.itchao.sellergoods.service.impl;

import cn.itchao.mapper.TbBrandMapper;
import cn.itchao.pojo.TbBrand;
import cn.itchao.pojo.TbBrandExample;
import cn.itchao.sellergoods.service.BrandService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author: jinchao
 * @date: 18/7/31 12:18
 * @description: 品牌接口实现类
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper tbBrandMapper;

    /**
     * @description:分页查询品牌
     * @param: pageNum
     * @param pageSize
     * @return: PageResult
     * @author: jinchao
     * @date:   18/7/31 12:25
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        //分页插件设置当前页和每页记录数
        PageHelper.startPage(pageNum,pageSize);
        //根据分页参数查询品牌列表
        Page<TbBrand> page = (Page<TbBrand>) tbBrandMapper.selectByExample(null);
        //返回品牌分页包装类
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * @description: 添加品牌
     * @param:  tbBrand 品牌实体对象
     * @return: void
     * @author: jinchao
     * @date:   18/7/31 14:07
     */
    @Override
    public void add(TbBrand brand) {
       this.tbBrandMapper.insert(brand);
    }
    /**
     * @description:  根据品牌编号查询品牌信息
     * @param:   id   品牌编号
     * @return:  TbBrand 品牌对象
     * @author: jinchao
     * @date:   18/7/31 14:59
     */
    @Override
    public TbBrand findOne(long id) {
        return this.tbBrandMapper.selectByPrimaryKey(id);
    }
    /**
     * @description: 修改品牌
     * @author: jinchao
     * @date: 18/7/31 11:41
     */
    @Override
    public void update(TbBrand brand) {
        this.tbBrandMapper.updateByPrimaryKey(brand);
    }
    /**
     * @description:批量删除品牌
     * @param:  ids
     * @author: jinchao
     * @date:   18/7/31 15:54
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id:ids) {
            this.tbBrandMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public PageResult findPage(TbBrand brand, int pageNum, int pageSize) {
        //分页插件设置当前页和每页记录数
        PageHelper.startPage(pageNum,pageSize);

        TbBrandExample brandExample = new TbBrandExample();

        TbBrandExample.Criteria criteria = brandExample.createCriteria();

        if(brand!=null){
            if(!StringUtils.isEmpty(brand.getName())){
                criteria.andNameLike("%"+brand.getName()+"%");
            }
            if(!StringUtils.isEmpty(brand.getFirstChar())){
                criteria.andFirstCharLike("%"+brand.getFirstChar()+"%");
            }
        }

        Page<TbBrand> page = (Page<TbBrand>) tbBrandMapper.selectByExample(brandExample);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Map> selectOptionList() {
        return tbBrandMapper.selectOptionList();
    }
}
